{{ define "content" }}
<div class="row" id="workers">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">
            <div class="x_title">
                <h2>{{ i18n "Workers info" . }}</h2>
                <ul class="nav navbar-right panel_toolbox">
                    <li>
                        <a href="javascript:refreshTables()">
                            <i class="fas fa-redo" title="{{ i18n "Refresh data" . }}" data-toggle="tooltip" data-placement="bottom"></i>
                        </a>
                    </li>
                </ul>
                <div class="clearfix"></div>
            </div>
            <div class="x_content">
                <div class="table-responsive">
                    <table class="table table-striped dt-responsive nowrap" style="width:100%">
                        <thead>
                        <tr>
                            <th>{{ i18n "Name" . }}</th>
                            <th>{{ i18n "Status" . }}</th>
                            <th>{{ i18n "Actions" . }}</th>
                            <th>{{ i18n "Attempts" . }}</th>
                            <th>{{ i18n "Last run duration" . }}</th>
                            <th>{{ i18n "Last run" . }}</th>
                            <th>{{ i18n "First run" . }}</th>
                            <th>{{ i18n "Next run" . }}</th>
                            <th>{{ i18n "ID" . }}</th>
                        </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
{{ end }}

{{ define "head" }}
    {{ staticHTML (staticURL "/dashboard/assets/vendors/datatables.net-bs/css/dataTables.bootstrap.min.css" false) }}
    {{ staticHTML (staticURL "/dashboard/assets/vendors/datatables.net-fixedheader-bs/css/fixedHeader.bootstrap.min.css" false) }}
    {{ staticHTML (staticURL "/dashboard/assets/vendors/datatables.net-responsive-bs/css/responsive.bootstrap.min.css" false) }}
{{ end }}

{{ define "js" }}
    {{ staticHTML (staticURL "/dashboard/assets/vendors/datatables.net/js/jquery.dataTables.min.js" false) }}
    {{ staticHTML (staticURL "/dashboard/assets/vendors/datatables.net-bs/js/dataTables.bootstrap.min.js" false) }}
    {{ staticHTML (staticURL "/dashboard/assets/vendors/datatables.net-fixedheader/js/dataTables.fixedHeader.min.js" false) }}
    {{ staticHTML (staticURL "/dashboard/assets/vendors/datatables.net-responsive/js/dataTables.responsive.min.js" false) }}
    {{ staticHTML (staticURL "/dashboard/assets/vendors/datatables.net-responsive-bs/js/responsive.bootstrap.min.js" false) }}

    <script type="application/javascript">
        $(document).ready(function () {
            var tableWorkers = $('#workers table')
                .DataTable({
                    stateSave: true,
                    stateDuration: 0,
                    pageLength: 50,
                    language: {
                        url: '/dashboard/datatables/i18n.json?locale=' + window.shadowLocale
                    },
                    ajax: {
                        url: '/boggart/workers/?action=list',
                        dataSrc: 'data'
                    },
                    columns: [{
                        data: 'name'
                    },{
                        data: 'status'
                    },{
                        data: null,
                        render: function (data, type, row) {
                            return '<div class="btn-group" role="group">' +
                                '<a href="/boggart/workers/?id=' + row.id + '&action=run" target="_blank" class="btn btn-success btn-icon btn-xs">' +
                                    '<i class="fas fa-running" title="{{ i18n "Execute" $ }}"></i>' +
                                '</a>' +
                                '<button type="button" class="btn btn-info btn-icon btn-xs" data-toggle="modal" data-target="#modal" data-modal-title="Confirm recalculate worker #' + row.id + '" data-modal-callback="workerAction(\'recalculate\', \'' + row.id + '\');">' +
                                    '<i class="fas fa-clock" title="{{ i18n "Recalculate" $ }}"></i>' +
                                '</button>' +
                                '<button type="button" class="btn btn-danger btn-icon btn-xs" data-toggle="modal" data-target="#modal" data-modal-title="Confirm unregister worker #' + row.id + '" data-modal-callback="workerAction(\'unregister\', \'' + row.id + '\');">' +
                                    '<i class="fas fa-trash" title="{{ i18n "Unregister" $ }}"></i>' +
                                '</button>' +
                            '</div>';
                        }
                    },{
                        data: null,
                        render: function (data, type, row) {
                            return '<div class="btn-group" role="group" style="min-width:96px">' +
                                 '<button class="btn btn-xs btn-success" title="{{ i18n "Count success attempts" . }}">' + row.attempts_success + '</button>' +
                                 '<button class="btn btn-xs btn-danger" title="{{ i18n "Count fails attempts" . }}">' + row.attempts_fails + '</button>' +
                            '</div>';
                        }
                    },{
                        data: null,
                        render: function (data, type, row) {
                            return typeof row.last_run_duration !== 'undefined' && row.last_run_duration.length > 0 ? row.last_run_duration : '{{ i18n "None" . }}';
                        }
                    },{
                        data: null,
                        render: function (data, type, row) {
                            return typeof row.last_run_at !== 'undefined' && row.last_run_at.length > 0 ? dateToString(row.last_run_at) : '{{ i18n "None" . }}';
                        }
                    },{
                        data: null,
                        render: function (data, type, row) {
                            return typeof row.first_run_at !== 'undefined' && row.first_run_at.length > 0 ? dateToString(row.first_run_at) : '{{ i18n "None" . }}';
                        }
                    },{
                        data: null,
                        render: function (data, type, row) {
                            return typeof row.next_run_at !== 'undefined' && row.next_run_at.length > 0 ? dateToString(row.next_run_at) : '{{ i18n "None" . }}';
                        }
                    },{
                        data: 'id'
                    }],
                    initComplete: function() {
                        var vars = {};
                        window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
                            vars[key] = value;
                        });

                        if (!vars.hasOwnProperty('search')) {
                            vars['search'] = '';
                        }

                        this.api().search(vars['search']).draw();
                    }
                });

            window.workerAction = function (action, id) {
                $.ajax({
                    type: 'POST',
                    url: '/boggart/workers/?action=' + action + '&id=' + id,
                    success: function (r) {
                        if (r.result === 'failed') {
                            new PNotify({
                                title: 'Error',
                                text: r.message,
                                type: 'error',
                                hide: false,
                                styling: 'bootstrap3'
                            });
                        } else if (r.message !== 'undefined' && r.message.length > 0) {
                            new PNotify({
                                title: 'Success',
                                text: r.message,
                                type: 'success',
                                hide: false,
                                styling: 'bootstrap3'
                            });
                        }

                        refreshTables();
                    }
                });
            };

            window.refreshTables = function () {
                tableWorkers.ajax.reload();
            };
        });
    </script>
{{ end }}
